package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.PageSearchDTO;
import com.qf.entity.MallGoods;
import com.qf.entity.ViewGoods;
import com.qf.mapper.GoodsMapper;
import com.qf.service.IGoodsService;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>title: com.qf.service.impl</p>
 * <p>Company: wendao</p>
 * author zhuximing
 * date 2021/9/18
 * description:
 */
@Service
public class GoodsService implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageResult<ViewGoods> selectByPage(PageSearchDTO<MallGoods> pageSearchDTO) {

        if (pageSearchDTO == null) {
            return  new PageResult<>(false, "参数不合法");
        }

        //设置分页信息
        PageHelper.startPage(pageSearchDTO.getPage(), pageSearchDTO.getSize());

        List<ViewGoods> byPage = goodsMapper.findGoodsByPage( pageSearchDTO.getWhere());

        //获取分页信息
        PageInfo<ViewGoods> pageInfo = new PageInfo<>(byPage);

        PageResult<ViewGoods> pageResult = new PageResult<>(true,"success");

        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());



        return pageResult;
    }

    @Override
    public Result delete(ViewGoods viewGoods) {
        Long spuId = viewGoods.getSpuId();

        if (spuId == 0) {
            return  new Result(false, "删除失败");
        }

        int delete = goodsMapper.delete(spuId);

        return new Result(delete==1?true:false, delete==1?"success":"fail");
    }
}